﻿using blog.Model;
using Microsoft.AspNetCore.Mvc;
using System.Web.Http.Controllers;

namespace blog.Controllers
{
    [Route("api/[action]")]
    [ApiController]
    public class FileController : ControllerBase
    {

       
        [HttpPost]
        public async Task<Result<string>> UploadImage(IFormFile file)
        {
            Result<string> res = new Result<string>();
            string fileName = Path.GetRandomFileName() + Path.GetExtension(file.FileName);
            var filePath = Path.Combine("D:\\.net\\net_blog\\public\\image", fileName);
            using (var fs = System.IO.File.Create(filePath))
            {
                await file.CopyToAsync(fs);

            }
            res.Code = 200;
            res.Message = "OK";
            res.Data = "\\image\\" + fileName;
            return res;
        }

        [HttpDelete]
        public static bool FilePicDelete(string path)
        {
            bool ret = false;
            System.IO.FileInfo file = new System.IO.FileInfo(path);

            if (file.Exists)
            {
                file.Delete();
                ret = true;
            }
            return ret;
        }
    }
}
